﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using PdfRotator.ViewModel;

namespace PdfRotator.Util
{
    class EnumUtil
    {
        public static T Parse<T>(string enumValue) where T : struct
        {
            T result;
            return Enum.TryParse(enumValue, true, out result) ? result : default(T);
        }

        public static IEnumerable<T> GetValues<T>()
        {
            return Enum.GetValues(typeof(T)).Cast<T>();
        }

        public static string GetDescription<T>(T value)
        {
            var field = value.GetType().GetField(value.ToString());
            var attr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
            return attr == null ? value.ToString() : attr.Description;
        }

        private static EnumViewModel<T> GetViewModel<T>(T value)
        {
            return new EnumViewModel<T> { Value = value, Description = GetDescription(value) };
        }

        public static List<EnumViewModel<T>> GetViewModels<T>()
        {
            return GetValues<T>().Select(GetViewModel).ToList();
        }
    }
}
